this.name = "Oolite Shipyards Extension";
this.author = "Paul Wilkins";
this.copyright = "(C) 2009 Paul Wilkins";
this.description = "Converted from the 0.70 version of ose.js to use proper ship loading techniques";
this.version = "0.71";

var ANARCHY = 0,
        FEUDAL = 1,
        MULTIGOVERNMENT = 2,
        DICTATORSHIP = 3,
        COMMUNIST = 4,
        CONFEDERACY = 5,
        DEMOCRACY = 6,
        CORPORATESTATE = 7;
var RICHINDUSTIAL = 0,
        AVERAGEINDUSTRIAL = 1,
        POORINDUSTRIAL = 2,
        MAINLYINDUSTRIAL = 3,
        MAINLYAGRICULTURAL = 4,
        RICHAGRICULTURAL = 5,
        AVERAGEAGRICULTURAL = 6,
        POORAGRICULTURAL = 7;

// startUp only runs once at startup before the demoscreen shows up.
this.startUp = function()
{
    this.reset(); // this.reset() fires not for a cmdr Jameson. When needed for a Jameson, call it here.
}

// this runs after loading in a saved game. missionVariables are read in at this point.
this.reset = function()
{
        if (oolite.compareVersion("1.73.4") > 0) {
                log("script." + this.name, "This Oolite Shipyards Extension does not run under Oolite version " + oolite.versionString + ". Oolite Shipyards Extension is disabled.");
                delete this.shipWillDockWithStation;
                delete this.shipExitedWitchspace;
                delete this.shipLaunchedFromStation;
        } else {
                this.mustPopulate = true;
        }
   /* used for adding ships after a first launch.
   This way you don't have to time consuming check every launch if a ship was already there.
   */
}

this.shipDockedWithStation = function(station) {
        // another eventhandler could have launched the ship already when it fires before this script.
        if (player.ship.docked) {
                this.freeTradeZoneDocking(station);
        }
}

// Ships that are added to the system, and to interstellar space
this.setUpGlobalShips = function()
{
        this.addNavyPatrolShips();
        this.addMissionaryShips();
        this.addDredgerShip();
        this.addHardPirateShips();
        this.pimpMySystem();
}

// Ships to be added to the system
this.setUpSystemShips = function()
{
        this.addOoBayShips();
        this.addInraPatrolShips();
        this.addSIRFYardStation();
        this.addFrogRickshawShip();
        this.addMissionaryShips();
        this.addClipperShip();
        this.addDredgerShip();
        this.addExecutiveFlightShips();
        this.addSalezaShips();
        this.addSuperCobraShips();
        this.addIxianShips();
        this.addHardPirateShips();
        this.addFreeTradeZone();
        this.addPirateCove();
        this.pimpMySystem();
}

// add global and system ships
this.setUpShips = function () {
        this.setUpGlobalShips();
        if (system.isInterstellarSpace) {
                return;
        }
        this.setUpSystemShips();
};

this.shipExitedWitchspace = function()
{
        this.setUpShips();
}

this.shipLaunchedFromStation = function()
{
  /* The first launch after a restart the system must be populated after a launch.
  The other times it happens after a WitchspaceJump */
        if (this.mustPopulate) {
                this.mustPopulate = false;
                this.setUpSystemShips();
        }
}

this.addOoBayShips = function () {
        if (system.countShipsWithPrimaryRole('Auction-Station-Storage') <= 9) {
                system.legacy_addShipsAtPrecisely('Auction-Station-Node1', 1, 'pwp', [0, 0, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node2', 1, 'pwp', [0, 0, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node3', 1, 'pwp', [0, 0, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node4', 1, 'pwp', [-0.2, 0, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node5', 1, 'pwp', [-0.2, 0, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node6', 1, 'pwp', [-0.2, 0, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node7', 1, 'pwp', [-0.4, 0, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node8', 1, 'pwp', [-0.4, 0, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node9', 1, 'pwp', [-0.4, 0, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node10', 1, 'pwp', [0, 0.2, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node11', 1, 'pwp', [0, 0.2, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node12', 1, 'pwp', [0, 0.2, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node13', 1, 'pwp', [-0.2, 0.2, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Beacon', 1, 'pwp', [-0.2, 0.2, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node14', 1, 'pwp', [-0.2, 0.2, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node15', 1, 'pwp', [-0.4, 0.2, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node16', 1, 'pwp', [-0.4, 0.2, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node17', 1, 'pwp', [-0.4, 0.2, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node18', 1, 'pwp', [0, 0.4, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node19', 1, 'pwp', [0, 0.4, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node20', 1, 'pwp', [0, 0.4, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node21', 1, 'pwp', [-0.2, 0.4, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node22', 1, 'pwp', [-0.2, 0.4, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node23', 1, 'pwp', [-0.2, 0.4, 1.8]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node24', 1, 'pwp', [-0.4, 0.4, 1.4]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node25', 1, 'pwp', [-0.4, 0.4, 1.6]);
                system.legacy_addShipsAtPrecisely('Auction-Station-Node26', 1, 'pwp', [-0.4, 0.4, 1.8]);
        }
};
this.addInraPatrolShips = function () {
        if (system.techLevel >= 15) {
                system.legacy_addShipsAt('INRA', 1, 'pwu', [0, 0, 0.7]);
                system.legacy_addShipsAt('INRA', 1, 'pwu', [0, 0, 0.3]);
        }
};
this.addNavyPatrolShips = function () {
        system.legacy_addShipsAt('police', 2, 'pwu', [0, 0, 0.9]);
        system.legacy_addShipsAt('militarybig', 2, 'pwu', [0, 0, 0.8]);
        system.legacy_addShipsAt('interceptor', 2, 'pwu', [0, 0, 0.65]);
        system.legacy_addShipsAt('militarycarrier', 2, 'pwu', [0, 0, 0.5]);
        system.legacy_addShipsAt('militarymedium', 2, 'pwu', [0, 0, 0.2]);
        system.legacy_addShipsAt('hunter', 2, 'pwu', [0, 0, 0.1]);
};
this.addSIRFYardStation = function () {
        if (system.techLevel >= 11) {
                if (system.countShipsWithPrimaryRole('SIRF-YARD') === 0) {
                        system.legacy_addShipsAt('SIRF-YARD', 1, 'pwu', [1, 1, 3]);
                }
        }
};
this.addFrogRickshawShip = function () {
        if (system.economy <= MAINLYINDUSTRIAL) {
                system.legacy_addSystemShips('rickshaw', 1, 0.1);
                system.legacy_addSystemShips('rickshaw', 1, 0.9);
        }
};
this.addMissionaryShips = function () {
        if (player.ship.fuel >= 5 && Math.random() < 0.90) {
                system.legacy_addSystemShips('missionary', 1, 0.3);
                system.legacy_addSystemShips('missionary', 1, 0.7);
        }
};
this.addClipperShip = function () {
        if (system.techLevel >= 8 && Math.random() < 0.65) {
                system.legacy_addSystemShips('clipper-rescueship', 1, 0.9);
        }
};
this.addDredgerShip = function () {
        if (system.techLevel >= 9 && Math.random() < 0.10) {
                system.legacy_addShipsAt('dredger', 1, 'pwp', [0.3, 0.3, 3.5]);
                system.legacy_addShipsAt('dredger', 1, 'pwp', [0, 0, 1.5]);
        }
};
this.addExecutiveFlightShips = function () {
        if (system.countShipsWithPrimaryRole('trident') === 0) {
                if (system.government <= COMMUNIST && Math.random() < 0.25) {
                        system.legacy_addSystemShips('trident', 1, 0.5);
                }
                if (system.government >= CONFEDERACY && system.techLevel >= 7 && Math.random() < 0.75) {
                        system.legacy_addSystemShips('trident', 1, 0.4);
                }
                if (system.government >= CONFEDERACY && system.techLevel >= 10 && Math.random() < 0.20) {
                        system.legacy_addSystemShips('strelka', 1, 0.8);
                }
        }
};
this.addSalezaShips = function () {
        if (system.government === CORPORATESTATE && system.techLevel <= 9 && Math.random() < 0.60) {
                system.legacy_addSystemShips('rigel', 1, 0.3);
                system.legacy_addSystemShips('saiph', 2, 0.6);
        }
        if (system.government >= CONFEDERACY && Math.random() < 0.30) {
                system.legacy_addSystemShips('saiph', 1, 0.4);
        }
        if (system.government === DICTATORSHIP && system.techLevel >= 10 && Math.random() < 0.80) {
                system.legacy_addSystemShips('rigel', 1, 0.4);
        }
        if (system.government === CORPORATESTATE && system.techLevel >= 10 && Math.random() < 0.60) {
                system.legacy_addSystemShips('bellatrix', 1, 0.3);
        }
};
this.addSuperCobraShips = function () {
        system.legacy_addShipsAt('policesupercobra', 1, 'spu', [0, 0, 0.2]);
        system.legacy_addShipsAt('supercobratraderminerhunter', 1, 'spu', [0, 0, 0.9]);
        system.legacy_addShipsAt('militarysupercobra', 1, 'spu', [0, 0, 0.75]);
        system.legacy_addShipsAt('piratesupercobra', 1, 'pwu', [0, 0, 0.4]);
};
this.addIxianShips = function () {
        if (system.government >= FEUDAL) {
                if (system.population < 30 && Math.random() < 0.20) {
                        system.legacy_addShipsAt('ixian_ship', 1, 'pwu', [0, 0, 0.75]);
                }
                if (system.population < 25 && Math.random() < 0.50) {
                        system.legacy_addShipsAt('ixian_ship', 1, 'pwu', [0, 0, 0.45]);
                }
        }
};
this.addHardPirateShips = function () {
        // Add a base level of pirate ships
        if (system.government === ANARCHY) {
                if (Math.random() < 0.30) {
                        system.legacy_addSystemShips('hardpirate', 4, 0.1);
                }
                if (Math.random() < 0.20) {
                        system.legacy_addSystemShips('hardpirate', 2, 0.4);
                }
                if (Math.random() < 0.10) {
                        system.legacy_addSystemShips('hardpirate', 6, 0.6);
                }
                if (Math.random() < 0.05) {
                        system.legacy_addSystemShips('hardpirate', 4, 0.8);
                }
                if (Math.random() < 0.04) {
                        system.legacy_addSystemShips('hardpirate', 4, 0.9);
                }
        }
        if (system.government === FEUDAL) {
                if (Math.random() < 0.25) {
                        system.legacy_addSystemShips('hardpirate', 2, 0.2);
                }
                if (Math.random() < 0.15) {
                        system.legacy_addSystemShips('hardpirate', 2, 0.1);
                }
                if (Math.random() < 0.10) {
                        system.legacy_addSystemShips('hardpirate', 4, 0.5);
                }
                if (Math.random() < 0.05) {
                        system.legacy_addSystemShips('hardpirate', 2, 0.8);
                }
                if (Math.random() < 0.03) {
                        system.legacy_addSystemShips('hardpirate', 4, 0.9);
                }
        }
        if (system.government >= MULTIGOVERNMENT && system.government <= CONFEDERACY) {
                if (Math.random() < 0.10) {
                        system.legacy_addSystemShips('hardpirate', 2, 0.1);
                }
                if (Math.random() < 0.08) {
                        system.legacy_addSystemShips('hardpirate', 4, 0.4);
                }
                if (Math.random() < 0.06) {
                        system.legacy_addSystemShips('hardpirate', 2, 0.3);
                }
        }
        if (system.government === DEMOCRACY) {
                if (Math.random() < 0.10) {
                        system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.45, 0], 10000);
                }
                if (Math.random() < 0.05) {
                        system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.30, 0], 10000);
                }
                if (Math.random() < 0.03) {
                        system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.90, 0], 10000);
                }
        }
        if (system.government === CORPORATESTATE) {
                if (Math.random() < 0.05) {
                        system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.55, 0], 10000);
                }
                if (Math.random() < 0.03) {
                        system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.75, 0], 10000);
                }
        }
        // Add additional pirate ships
        if (system.government === ANARCHY && Math.random() < 0.30) {
                system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.3, 0], 10000);
        }
        if (system.government <= FEUDAL && Math.random() < 0.20) {
                system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.45, 0], 10000);
        }
        if (system.government <= MULTIGOVERNMENT && Math.random() < 0.10) {
                system.legacy_addShipsWithinRadius('hardpirate', 4, 'swu', [0, 0.65, 0], 10000);
        }
        if (system.government <= COMMUNIST && Math.random() < 0.05) {
                system.legacy_addShipsWithinRadius('hardpirate', 2, 'swu', [0, 0.75, 0], 10000);
        }
        if (system.government <= CONFEDERACY && Math.random() < 0.03) {
                system.legacy_addShipsWithinRadius('hardpirate', 4, 'swu', [0, 0.85, 0], 10000);
        }
        if (system.government >= DEMOCRACY && Math.random() < 0.04) {
                system.legacy_addSystemShips('hardpirate', 2, 0.5);
        }
        if (system.government >= DEMOCRACY && Math.random() < 0.03) {
                system.legacy_addSystemShips('hardpirate', 4, 0.3);
        }
};
this.addFreeTradeZone = function () {
        if (system.government === MULTIGOVERNMENT) {
                system.legacy_addShipsAt('free_trade_zone', 1, 'wpu', [0, 0, -0.3]);
                system.legacy_addShipsAtPrecisely('ftzpirate', 2, 'wpu', [0, 0, -0.25]);
                this.freetradezone_marked = false;
                if (Math.random() < 0.25) {
                        system.legacy_addShipsAtPrecisely('pirate', 2, 'wpu', [0, 0, -0.25]);
                }
                if (Math.random() < 0.70) {
                        system.legacy_addShipsAtPrecisely('ftzpirate', 2, 'wpu', [0, 0, -0.2]);
                }
                if (Math.random() < 0.30) {
                        system.legacy_addShipsAtPrecisely('ftzpirate', 4, 'wpu', [0, 0, -0.15]);
                }
                if (Math.random() < 0.50) {
                        system.legacy_addShipsWithinRadius('ftzhauler', 1, 'wpu', [0, 0, -0.2], 5000);
                }
                if (Math.random() < 0.25) {
                        system.legacy_addShipsWithinRadius('ftzhauler', 1, 'wpu', [0, 0, -0.25], 5000);
                }
                if (Math.random() < 0.10) {
                        system.legacy_addShipsWithinRadius('police', 6, 'wpu', [0, 0, -0.3], 5000);
                }
        }
};
this.freeTradeZoneDocking = function (station) {
        if (station.name === 'Free Trade Zone') {
                if (this.freetradezone_marked !== true) {
                        player.bounty += 5;
                        this.freetradezone_marked = true;
                }
        }
};
this.addPirateCove = function () {
        if (system.countShipsWithPrimaryRole('pirate') >= 10 && system.countShipsWithPrimaryRole('pirate-cove') === 0) {
                system.legacy_addSystemShips('pirate-cove', 1, 0.12);
                system.legacy_addSystemShips('asteroid', 8, 0.12);
        }
};
this.pimpMySystem = function () {
        system.legacy_addShipsAt('uber', 1, 'spu', [0, 0, 0.9]);
        system.legacy_addShipsAt('uber', 1, 'wpu', [0, 0, 0.5]);
        system.legacy_addShipsAt('uber', 1, 'wsu', [0, 0, 0.5]);
};